﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNManifestMenuBuildWorker
	{
		
		public static string MarginSize = "width:18px;";
		public static string TextSize = "width:220px;";
		
		//通过Manifest上的ContextMenuList构造Menu的Html内容.
		public static void  buildMenu(Manifest manifest)
		{
			//CID 0284 //2009.05.10 //Begin
			ArrayList viewList = manifest.ViewList;
			for (int i = 0; i < viewList.Count; i++)
			{
				View view = viewList[ i ] as View;
				Menu contextMenu = view.ContextMenu;

				XmlDocument tempDoc = new XmlDocument();
				XmlElement menuElement = tempDoc.CreateElement("div");
				menuElement.SetAttribute(JetConstant.HTML_id, JetConstant.MenuId);
				menuElement.SetAttribute("oncontextmenu", "InfoJet_DisableContextMenu(event);");
				tempDoc.AppendChild( menuElement );

				buildMenu(manifest, contextMenu, tempDoc, menuElement, i + "");

				view.ContextMenuHtml = tempDoc.OuterXml;
			}
			//CID 0284 //2009.05.10 //End
		}
		
		private static void buildMenu(Manifest manifest, Menu menu, XmlDocument tempDoc, XmlElement menuElement, string id)
		{
			XmlElement groupElement = tempDoc.CreateElement("table");
			groupElement.SetAttribute(JetConstant.HTML_id, menu.Id + "group" + id);
			//CID 0071 //2008.07.28 //Begin
			groupElement.SetAttribute("style", "Font-FAMILY:'Tahoma','Verdana','Arial'; FONT-SIZE: 9pt;padding:1px 1px 1px 1px;BORDER:1px solid #8A867A; DISPLAY: none; Z-INDEX: 99; LEFT: 100px;POSITION: absolute; TOP: 100px; BACKGROUND-COLOR: #FFFFFF;white-space:nowrap;");
			//CID 0071 //2008.07.28 //End
			groupElement.SetAttribute("xdoc_menu", "yes");
			groupElement.SetAttribute("cellspacing", "0");
			groupElement.SetAttribute("cellpadding", "0");
			menuElement.AppendChild(groupElement);
			
            //CID 1235 //2012.08.05 //Begin
            /*
			XmlElement headerElement = tempDoc.CreateElement("tr");
			groupElement.AppendChild(headerElement);
			XmlElement headerTDElement = tempDoc.CreateElement("td");
			headerTDElement.SetAttribute("colspan", "3");
			headerElement.AppendChild(headerTDElement);
            */
            //CID 1235 //2012.08.05 //End

			ArrayList subMenuList = menu.SubMenuList;
			if (subMenuList != null)
			{
				foreach (Menu subMenu in subMenuList)
				{
					if (subMenu.Type.Equals(JetConstant.MNU_BUTTON))
					{
						buildButton(manifest, subMenu, tempDoc, groupElement, id);
					}
					else if (subMenu.Type.Equals(JetConstant.MNU_MENU))
					{
						buildButton(manifest, subMenu, tempDoc, groupElement, id);
						if (subMenu.SubMenuList != null && subMenu.SubMenuList.Count > 0)
						{
							buildMenu(manifest, subMenu, tempDoc, menuElement, id);
						}
					}
					else if (subMenu.Type.Equals(JetConstant.MNU_SEPARATOR))
					{
						buildSeparator(manifest, subMenu, tempDoc, groupElement, id);
					}
				}
			}
			
			//CID 1235 //2012.08.05 //Begin
            /*
			XmlElement footerElement = tempDoc.CreateElement("tr");
			groupElement.AppendChild(footerElement);
			XmlElement footerTDElement = tempDoc.CreateElement("td");
			footerTDElement.SetAttribute("colspan", "3");
			footerElement.AppendChild(footerTDElement);
            */
            //CID 1235 //2012.08.05 //End
		}
		
		private static void buildSeparator(Manifest manifest, Menu separator, XmlDocument tempDoc, XmlElement tableElement, string id)
		{
			XmlElement sepElement = tempDoc.CreateElement("tr");
			sepElement.SetAttribute(JetConstant.HTML_id, separator.Id + "menu" + id);
			sepElement.SetAttribute("action", JetConstant.MNU_ACT_NONE);
			tableElement.AppendChild(sepElement);
			
			XmlElement tdElement1 = tempDoc.CreateElement("td");
			tdElement1.SetAttribute("style", manifest.ImageUri.MenuSepLeft);
			sepElement.AppendChild(tdElement1);
			
			XmlElement tdElement2 = tempDoc.CreateElement("td");
			tdElement2.SetAttribute("style", manifest.ImageUri.MenuSepMiddle);
			sepElement.AppendChild(tdElement2);
			
			XmlElement tdElement3 = tempDoc.CreateElement("td");
			tdElement3.SetAttribute("style", manifest.ImageUri.MenuSepRight);
			sepElement.AppendChild(tdElement3);
		}
		
		private static void buildButton(Manifest manifest, Menu button, XmlDocument tempDoc, XmlElement tableElement, string id)
		{
			XmlElement trElement = tempDoc.CreateElement("tr");
			trElement.SetAttribute(JetConstant.HTML_id, button.Id + "menu" + id);
			trElement.SetAttribute("onclick", "InfoJet_OnMenuClick(event,this);");
			trElement.SetAttribute("onmouseover", "InfoJet_MenuOver(event,this);");
			trElement.SetAttribute("onmouseout", "InfoJet_MenuOut(event,this);");
			if (button.SubMenuList != null && button.SubMenuList.Count > 0)
			{
				trElement.SetAttribute("type", "group");
			}
			else
			{
				trElement.SetAttribute("type", "item");
				trElement.SetAttribute("xmlToEdit", button.XmlToEdit);
				trElement.SetAttribute("component", button.Component);
				trElement.SetAttribute("action", button.Action);
			}
			//CID 0071 //2008.07.28 //Begin
			trElement.SetAttribute("style", "border:1px solid #FFFFFF;padding:1px 1px 1px 1px;DISPLAY: block; CURSOR: default;white-space:nowrap;");
			//CID 0071 //2008.07.28 //End
			trElement.SetAttribute("xdoc_menu", "yes");
			tableElement.AppendChild(trElement);
			
			XmlElement checkElement = tempDoc.CreateElement("td");
			checkElement.SetAttribute(JetConstant.HTML_id, button.Id + "check" + id);
			checkElement.SetAttribute("style", MarginSize);
			checkElement.SetAttribute("xdoc_menu", "yes");
			trElement.AppendChild(checkElement);

			//CID 0284 //2009.05.10 //Begin
			if( button.Component != null && button.Component.Equals( id ) )
			{
				XmlElement divElement = tempDoc.CreateElement("div");
				divElement.SetAttribute("align", "center");
				checkElement.AppendChild(divElement);
				XmlElement imgElement = tempDoc.CreateElement("img");
				imgElement.SetAttribute("src", manifest.ImageUri.MenuCheck);
				divElement.AppendChild(imgElement);
			}
			//CID 0284 //2009.05.10 //End
			
			XmlElement textElement = tempDoc.CreateElement("td");
			textElement.SetAttribute(JetConstant.HTML_id, button.Id + "text" + id);
			textElement.InnerText  = button.Caption;
			//CID 0071 //2008.07.28 //Begin
			textElement.SetAttribute("style", TextSize + "Font-FAMILY:'Tahoma','Verdana','Arial'; FONT-SIZE: 9pt;" );
			//CID 0071 //2008.07.28 //End
			textElement.SetAttribute("xdoc_menu", "yes");
			trElement.AppendChild(textElement);
			
			XmlElement arrowElement = tempDoc.CreateElement("td");
			arrowElement.SetAttribute(JetConstant.HTML_id, button.Id + "arrow" + id);
			arrowElement.SetAttribute("style", MarginSize);
			arrowElement.SetAttribute("align", "right");
			arrowElement.SetAttribute("xdoc_menu", "yes");
			trElement.AppendChild(arrowElement);
			
			if (button.Type.Equals(JetConstant.MNU_MENU))
			{
				XmlElement imgElement = tempDoc.CreateElement("img");
				imgElement.SetAttribute("src", manifest.ImageUri.MenuGroup);
				arrowElement.AppendChild(imgElement);
			}
		}
	}
}